import { createRouter, createWebHashHistory } from "vue-router"

export const routes = [
    {
        path: '/login',
        component: () => import('@/views/login/index.vue'),
        name: 'login',
        meta: {
            title: "登录",
            hidden: true
        }
    },
    {
        path: '/',
        component: () => import('@/layout/main.vue'),
        name: 'layout',
        meta: {
            title: "layout",
            hidden: false,
            icon: "House"
        },
        redirect: "/home",//重定向到首页
        children: [
            {
                path: "/home",
                component: () => import('@/views/home/index.vue'),
                meta: {
                    title: "首页",
                    icon: "House"
                },
            },
        ]
    },
    {
        path: '/user',
        component: () => import('@/layout/main.vue'),
        name: 'user',
        meta: {
            title: "用户管理",
            hidden: false,
            icon: "User"
        },
        children: [
            {
                path: "/user",
                component: () => import('@/views/user/index.vue'),
                meta: {
                    title: "用户列表",
                    icon: "User"
                },
            },
            {
                path: "/form1",
                component: () => import('@/views/form/index.vue'),
                meta: {
                    title: "表单2",
                    icon: "Iphone"
                }
            },
            {
                path: "/ai",
                component: () => import('@/views/deepseek/index.vue'),
                meta: {
                    title: "测试",
                    icon: "Iphone"
                }
            },
            {
                path: "/jsx",
                component: () => import('@/views/jsx/index.jsx'),
                meta: {
                    title: "JSX",
                    icon: "Iphone"
                }
            },
            {
                path: "/dynamicComponent",
                component: () => import('@/views/test/dynamicComponent.vue'),
                meta: {
                    title: "动态组件",
                    icon: "IceCreamRound"
                }
            },

        ]
    },
    {
        path: '/form',
        component: () => import('@/layout/main.vue'),
        name: 'form',
        meta: {
            title: "表单",
            hidden: false,
            icon: "House"
        },
        children: [
            {
                path: "/test",
                component: () => import('@/views/form/test.vue'),
                meta: {
                    title: "表单组件",
                    icon: "IceCreamRound"
                },
            },
            {
                path: "/test/index",
                component: () => import('@/views/test/index.vue'),
                meta: {
                    title: "测试组件",
                    icon: "IceCreamRound"
                },
            },
        ]
    },
    {
        path: '/userInfo',
        component: () => import('@/layout/main.vue'),
        name: 'userInfo',
        meta: {
            title: "用户信息",
            hidden: true,
            icon: "User"
        },
        redirect: "/info",
        children: [
            {
                path: "/info",
                component: () => import('@/views/user/info.vue'),
                meta: {
                    title: "个人信息",
                    icon: "User",
                    hidden: true,
                },
            },
        ]
    },

    {
        path: '/404',
        component: () => import('@/views/404/index.vue'),
        name: '404',
        meta: {
            title: "404",
            hidden: true
        }
    },
    {
        path: '/:catchAll(.*)',
        redirect: '/404',
        name: 'any',
        meta: {
            title: "任意路由",
            hidden: true
        }
    },
]


const router = createRouter({

    history: createWebHashHistory(),
    routes,

    scrollBehavior() {
        return {
            left: 0,
            top: 0,
        }
    }
})
router.beforeEach((to, from, next) => {
    let token = localStorage.getItem("token");
    console.log(token, "token")
    if (token) {
        next();
    } else {
        if (to.path == "/login") {
            next();
        } else {
            next({ path: "/login" });
        }
    }
});
export default router;